package com.nova.android.tvlauncher.ui.dialog

import android.content.Context
import android.graphics.Color
import androidx.core.view.isVisible
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.databinding.PopupLayoutBinding
import com.lxj.xpopup.core.BubbleAttachPopupView
import com.lxj.xpopup.util.XPopupUtils
import com_github_lib_base.ext.clickNoRepeat
import com_github_lib_base.ext.stringValue

class HomeMenuPop(
    context: Context,
    val setup: (HomeMenuPop.() -> Unit)? = null
) : BubbleAttachPopupView(context) {

    private val mBind: PopupLayoutBinding

    init {
        addInnerContent()
        mBind = PopupLayoutBinding.bind(popupImplView)
    }

    override fun getImplLayoutId() = R.layout.popup_layout

    var isInfoUninstallVisible = false
    var isDisableUninstall = false

    // 是否已存在于收藏夹中了
    var isFavorited = true

    override fun initPopupContent() {
        super.initPopupContent()
        setup?.invoke(this)
        initBubble()
        mBind.apply {

            if(isInfoUninstallVisible) {
                tvAppInfo.isVisible = true
                tvUninstall.isVisible = true
                tvMove.isVisible = false
            }

            if(isFavorited) {
                tvRemove.text = com.nova.lib_lang.R.string.main_pop_txt_4.stringValue()
                //tvRemove.text = "从收藏夹中移除"
            }else {
                tvRemove.text = com.nova.lib_lang.R.string.main_pop_txt_3.stringValue()
                //tvRemove.text = "添加到收藏夹"
            }

            if(isDisableUninstall) {
                tvUninstall.isEnabled = false
                tvUninstall.setImageInView(1, R.drawable.icon_uninstall_disable)
            }else {
                tvUninstall.isEnabled = true
                tvUninstall.setImageInView(1, R.drawable.icon_uninstall_normal)
            }

            tvOpen.clickNoRepeat {
                clickOpen?.invoke()
                dismiss()
            }
            tvMove.clickNoRepeat {
                clickMove?.invoke()
                dismiss()
            }
            tvRemove.clickNoRepeat {
                clickRemove?.invoke(isFavorited)
                dismiss()
            }

            tvAppInfo.clickNoRepeat {
                clickAppInfo?.invoke()
                dismiss()
            }

            tvUninstall.clickNoRepeat {
                clickUninstall?.invoke()
                dismiss()
            }
        }
    }

    private var clickOpen: (() -> Unit)? = null
    fun clickOpen(listener: (() -> Unit)?) {
        clickOpen = listener
    }

    private var clickMove: (() -> Unit)? = null
    fun clickMove(listener: (() -> Unit)?) {
        clickMove = listener
    }

    private var clickRemove: ((isFavorited: Boolean) -> Unit)? = null
    fun clickRemove(listener: ((isFavorited: Boolean) -> Unit)?) {
        clickRemove = listener
    }

    private var clickAppInfo: (() -> Unit)? = null
    fun clickAppInfo(listener: (() -> Unit)?) {
        clickAppInfo = listener
    }

    private var clickUninstall: (() -> Unit)? = null
    fun clickUninstall(listener: (() -> Unit)?) {
        clickUninstall = listener
    }

    private fun initBubble() {
        setBubbleBgColor(Color.WHITE)
        setArrowWidth(XPopupUtils.dp2px(context, 5f))
        setArrowHeight(XPopupUtils.dp2px(context, 5f))
            .setBubbleRadius(XPopupUtils.dp2px(context, 5f))
        setArrowRadius(XPopupUtils.dp2px(context, 0f))
    }

    companion object {
        var dialog: HomeMenuPop? = null

        @JvmStatic
        fun newInstance(
            context: Context,
            setup: (HomeMenuPop.() -> Unit)? = null
        ): HomeMenuPop? {
            dialog?.dismiss()
            dialog = null
            dialog = HomeMenuPop(context, setup)
            return dialog
        }
    }


}